package com.bsoft.controller.JSON;

import com.bsoft.Exception.CommonException;
import com.bsoft.bean.DesDecrypt;
import com.bsoft.service.EcgService;
import com.bsoft.service.RegisterService;
import com.bsoft.utils.JSONUtil;
import com.bsoft.utils.XmlMapUtils;
import net.sf.json.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.apache.ibatis.annotations.Param;
import org.dom4j.DocumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**简阳空分/金堂第一人民医院
 * 邦健单机版心电系统标准接口
 */
@Controller
@RequestMapping(value = "JSON/ECG")
public class EcgController {
    private static Logger logger = LoggerFactory.getLogger(EcgController.class);

    @Autowired
    private EcgService ecgService;

    /**
     * 2.2 获取病人信息接口
     */
    @RequestMapping(value = "getPatientInfo", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    @ResponseBody
    public String getPatientInfo(@RequestParam("applyId") String applyId)  {
        logger.info("心电:获取病人信息接口:入参:" + applyId);
        String res ="";
        try {
             res = ecgService.getPatientInfo(applyId);
        }catch (Exception e){
            ecgHandleException(e);
        }

        logger.info("心电:获取病人信息接口:出参:" + res);
        return res;
    }


    /**简阳空分 / 金堂人民医院
     * 2.5 回传诊断报告接口(HIS)
     */
    @RequestMapping(value = "publishResult", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public String publishResult(@RequestBody Map<String, Object> req) {
        String s = JSONUtil.map2Json(req);
        logger.info("心电:回传诊断报告接口:入参:" + s);
        String res ="";
        try {
            res = ecgService.publishResult(req);
        }catch (Exception e){
            ecgHandleException(e);
        }
        logger.info("心电:回传诊断报告接口:出参:" + res);
        return res;
    }


    /**
     * 2.5 回传诊断报告接口(HIS)
     */
    @RequestMapping(value = "uploadImage", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public String uploadImage(@RequestBody Map<String, Object> req) {
        logger.info("心电:报告图片处理:入参:" + req);
        String res ="";
        try {
            res = ecgService.uploadImage(req);
        }catch (Exception e){
            ecgHandleException(e);
        }
        logger.info("心电:报告图片处理:出参:" + res);
        return res;
    }


    public Object ecgHandleException(Exception e) {
        logger.error("简阳:心电系统:异常:", e);
        JSONObject json = new JSONObject();
        json.put("data", null);
        json.put("code", 99);
        json.put("msg", "系统错误:"+e.getMessage());
        logger.info("简阳:心电系统:异常:出参:" + json.toString());
        return json;
    }

}
